#pragma once

#include "Thread.hpp"
#include "BlockQueue.hpp"
#include <vector>
#define NUM 5

namespace yui
{
    template <class T>
    class ThreadPool
    {
    private:
    public:
        ThreadPool(int num = NUM)
            : _threadNum(num)
        {
        }
        ~ThreadPool()
        {
            for (auto &x : _threads)
            {
                x.Join();
            }
        }
        void init()
        {
            for (int i = 0; i < _threadNum; ++i)
            {
                std::string name = "thread-" + std::to_string(i + 1);
                _threads.emplace_back(std::bind(&ThreadPool::HanderTask, this, std::placeholders::_1), name);
            }
        }
        void start()
        {
            for (auto &x : _threads)
            {
                x.Start();
            }
        }
        void HanderTask(std::string name)
        {
            pthread_detach(pthread_self());
            while(true)
            {

                T task;
                _BQTasks.pop(&task);
                cout<<"取到任务："<<task<<endl;
            }
        }
        void pushTask(const T&task)
        {
            _BQTasks.push(task);
        }

    private:
        std::vector<yui::Thread> _threads;
        yui::BlockQueue<T> _BQTasks;
        int _threadNum;
    };
}